<template>
	<view class="container">
		<!-- 搜索栏 -->
		<view class="search-bar">
			<input type="text" placeholder="订单编号" name="documentNumber" id="documentNumber" v-model="documentNumber" />
			<button class="search-btn" @click="selectOrderNo()">搜索</button>
		</view>

		<!-- 订单列表 -->
		<scroll-view class="log-list" scroll-y>
			<view @click="selectLogDetails(item.salesId)" class="log-item" v-for="(item, index) in logData" :key="index">
				<view class="log-details">
					<text class="log-order">{{ item.storeName }}</text>&nbsp;
					<text style="color: #000000;">新增订单：</text>
					<text class="log-order">{{ item.documentNumber }}</text>
				</view>
				<view class="log-header">
					<text class="log-time">{{ formatTime(item.createdAt) }}</text>
				</view>
			</view>
		</scroll-view>

		<!-- 提示 -->
		<view class="footer">
			<text>提示：仅支持查看近3个月的操作日志数据</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				logData: [],
				orderDetails: [],
				orderNo: [],
				documentNumber: ''
			};
		},
		methods: {
			formatTime(timeString) {
						const date = new Date(timeString);
						const year = date.getFullYear();
						const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需+1
						const day = String(date.getDate()).padStart(2, '0');
						const hours = String(date.getHours()).padStart(2, '0');
						const minutes = String(date.getMinutes()).padStart(2, '0');
						return `${year}-${month}-${day} ${hours}:${minutes}`;
					},
			  selectLogDetails(salesId) {
			        if (this.isNavigating) return;
			        this.isNavigating = true; // 加锁
			        const app = getApp();
			        const url = app.globalData.url;
			        uni.request({
			            url: url + "SALE-SERVICE/sale/selectSalesOrderByIdWX?salesId=" + salesId,
			            method: "GET",
			            success: (res) => {
			                if (res.statusCode === 200) {
			                    uni.navigateTo({
			                        url: "/pages/logDetails/logDetails?salesId=" + salesId,
			                        complete: () => this.isNavigating = false // 解锁
			                    });
			                }
			            },
			            fail: () => {
			                this.isNavigating = false; // 解锁
			            }
			        });
			    },
			selectOrderAll() {
				const app = getApp();
				const url = app.globalData.url;
				uni.request({
					url: url + 'SALE-SERVICE/sale/selectSalesOrderWX',
					method: "GET",
					success: (res) => {
						if (res.statusCode === 200) {
							this.logData = res.data;
							console.log(this.logData = res.data)
						} else {

							uni.showToast({
								title: "加载失败",
								icon: "none"
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: "网络请求错误",
							icon: "none"
						});
					}
				});
			},
			selectOrderNo() {
				const app = getApp();
				const url = app.globalData.url;
				uni.request({
					url: url + "SALE-SERVICE/sale/selectSalesByDocumentNumber?documentNumber=" + this
						.documentNumber,
					method: "GET",
					success: (res) => {
						if (res.statusCode === 200) {
							this.logData = res.data; // 假设你有一个变量来存储商品详情
							console.log(this.logData);
						} else {
							uni.showToast({
								title: "加载失败",
								icon: "none"
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: "网络请求错误",
							icon: "none"
						});
					}
				});
			},
		},
		onShow() {
			
			console.log("已刷新当前页面")
		},
		mounted() {
			this.selectOrderAll();
		
		}

	};
</script>

<style>
	.container {
		background-color: #f1f1f1;
		height: 100vh;
		padding: 0px;
	}

	.search-bar {
		display: flex;
		align-items: center;
		background-color: #fff;
		padding: 15px;
		border-radius: 5px;
		margin-bottom: 10px;
	}
	
	.search-bar input {
		flex: 1;
		padding: 8px;
		border: 1px solid #ddd;
		border-radius: 5px;
		font-size: 14px;
	}
	
	.search-btn {
		width: 70px;
		height: 40px;
		font-size: 15px;
		margin-left: 10px;
		background-color: #007aff;
		color: #fff;
		border: none;
		border-radius: 5px;
		cursor: pointer;
	}
	
	/* 订单列表 */
	.log-list {
		background-color: #f1f1f1; 
		border-radius: 5px;
		height: calc(100% - 180px); /* 动态计算高度 */
		padding:0px 10px 10px 10px; 
		box-sizing: border-box;
	}
	
	.log-item {
		background-color: #fff; 
		border-radius: 5px;
		padding: 10px;
		margin: 10px 0; 
		box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); 
	}
	
	.log-header {
		display: flex;
		justify-content: space-between;
		margin-top: 10px;
	}
	
	.log-time {
		font-size: 16px;
		color: #000000;
	}
	
	.log-details {
		display: flex;
		/* justify-content: space-between; */
		align-items: center;
	}
	
	.log-details text:first-child {
		font-size: 16px;
		color: #666;
	}
	
	.log-details .log-order {
		font-size: 16px;
		color: #000000;
	}
	
	/* 提示文字 */
	.footer {
		margin-top: 10px;
		font-size: 14px;
		color: #999;
		text-align: center;
	}
</style>